package dfl.bysj.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import dfl.bysj.config.ExcelDictDTOListener;
import dfl.bysj.config.ExcelDictDTOListener_teacher;
import dfl.bysj.entity.Academy;
import dfl.bysj.entity.Grade;
import dfl.bysj.entity.Student;
import dfl.bysj.entity.Teacher;
import dfl.bysj.entity.VO.ExcelDictDTO_Student;
import dfl.bysj.entity.VO.ExcelDictDTO_Teacher;
import dfl.bysj.mapper.TeacherMapper;
import dfl.bysj.service.TeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author DFL
 * @since 2022-05-17
 */
@Slf4j
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void importData(InputStream inputStream) {
        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
        EasyExcel.read(inputStream, ExcelDictDTO_Teacher.class, new ExcelDictDTOListener_teacher(this)).sheet().doRead();
        log.info("importData finished");
    }

    @Override
    public List<ExcelDictDTO_Teacher> listDictData() {

        List<Teacher> teacherList = baseMapper.selectList(null);

        //创建ExcelDictDTO列表，将Dict列表转换成ExcelDictDTO列表
        ArrayList<ExcelDictDTO_Teacher> excelDictDTOList = new ArrayList<>(teacherList.size());
        teacherList.forEach(teacher -> {


            ExcelDictDTO_Teacher excelDictDTO = new ExcelDictDTO_Teacher(teacher);
            excelDictDTOList.add(excelDictDTO);
        });
        return excelDictDTOList;
    }

    @Override
    public void insertBatch(List<ExcelDictDTO_Teacher> list) {
        log.info("insertBatch");
        List<Teacher> teacherList=new ArrayList<>();
        log.info("insertBatch=>list:{}",list.toString());
        list.stream()
                .forEach(s->{

                    teacherList.add(new Teacher(s));
                });
        saveBatch(teacherList);
    }

}
